import com.sun.source.tree.Tree;

import java.util.*;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    public static int i =0;
    public static TreeNode createTree(String str) {
        TreeNode root = null;
        if(str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }
    // 获取树中节点的个数
    public static int nodeSize;
    public void size(TreeNode root){
        if (root == null) {
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }
    // 子问题：树的节点 = 左子树的结点 + 右子树的节点 + 1
    public int size2(TreeNode root){
        if (root == null){
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }
    // 获取叶子节点的个数
    public static int leafNode;
    public void getLeafNodeCount(TreeNode root){
        if (root == null){
            return;
        }
        if (root.left == null && root.right == null){
            leafNode++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);

    }
    //子问题
    public int getLeafNodeCount2(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);

    }
    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if (root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return Math.max(leftHight,rightHight)+1;

    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val){
        if (root == null) {
            return null;
        }if (root.val == val){
            return root;
        }
        TreeNode leftNode = find(root.left,val);
        if (leftNode != null){
            return leftNode;
        }
        TreeNode rightNode = find(root.right,val);
        if (rightNode != null){
            return rightNode;
        }
        return null;
    }
    //层序遍历
    public void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if (cur.left != null){
                queue.offer(cur.left);
            }
            if (cur.right != null){
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }
    public List<List<Character>> levelOrder2(TreeNode root){
        List<List<Character>> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }
    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if (root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while (!queue.isEmpty()){
            TreeNode cur = queue.peek();
            if (cur != null){
                return false;
            }
            queue.poll();
        }
        return true;
    }
    // 前序遍历
    public void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        // 打印根节点
        System.out.print(root.val+" ");
        //左节点
        preOrder(root.left);
        //右节点
        preOrder(root.right);
    }
    public void preOrderNor(TreeNode root){
        if (root == null){
            return;
        }
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null||!stack.empty()){
            while (cur != null){
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
        System.out.println();
    }

    // 中序遍历
    public void inOrder(TreeNode root){
        if (root == null) {
            return;
        }
        //左节点
        inOrder(root.left);
        // 打印根节点
        System.out.print(root.val+" ");
        //右节点
        inOrder(root.right);
    }
    public void inOrderNor(TreeNode root){
        if (root == null){
            return;
        }
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null||!stack.empty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val+" ");
            cur = top.right;
        }
        System.out.println();
    }
    // 后序遍历
    public void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        //左节点
        postOrder(root.left);
        //右节点
        postOrder(root.right);
        // 打印根节点
        System.out.print(root.val+" ");
    }
    public void postOrderNor(TreeNode root){
        if (root == null){
            return;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null||!stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev){
                System.out.print(top.val +" ");
                stack.pop();
                prev = top;
            }else {
                cur = top.right;
            }
        }
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //1.结构上是否相同
        if ((p != null && q == null)||(p == null && q != null)){
            return false;
        }
        // 上述if语句不执行，说明 p 和 q 要么都为空，要么都不为空
        if (p == null && q == null){
            return true;
        }
        // 都不为空，判断值是否一样
        if (p.val != q.val){
            return false;
        }
        // 说明，左右都不为空，值也一样
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null){
            return false;
        }
        if (isSametree(root,subRoot)) return true;
        if (isSubtree(root.left,subRoot)) return true;
        if (isSubtree(root.right,subRoot)) return true;
        return false;
    }
    public boolean isSametree(TreeNode p,TreeNode q){
        if ((p != null && q == null) || (p == null && q != null)){
            return false;
        }
        if (p == null && q == null){
            return true;
        }
        if (p.val != q.val){
            return false;
        }
        return isSametree(p.left,q.left) && isSametree(p.right,q.right);
    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null){
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null){
            return false;
        }
        return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){
        if ((leftTree != null && rightTree == null) || (leftTree == null && rightTree != null)){
            return false;
        }
        if (leftTree == null && rightTree == null){
            return true;
        }
        if (leftTree.val != rightTree.val){
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);
    }
    public boolean isBalanced(TreeNode root) {
        if (root == null){
            return true;
        }
        int leftHight = getHight(root.left);
        int rightHight = getHight(root.right);
        return Math.abs(leftHight - rightHight) <= 1 && isBalanced(root.right) && isBalanced(root.left);
    }
    public int getHight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHight = getHight(root.left);
        int rightHight = getHight(root.right);
        return Math.max(leftHight,rightHight) + 1;
    }

    public boolean isBalanced2(TreeNode root) {
        if (root == null){
            return true;
        }
        return getHight2(root)>=0;
    }
    public int getHight2(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHight = getHight2(root.left);
        if (leftHight < 0){
            return -1;
        }
        int rightHight = getHight2(root.right);
        if (rightHight >= 0 &&Math.abs(leftHight - rightHight) <= 1){
            return Math.max(leftHight,rightHight) + 1;
        }else{
            return -1;
        }
    }
    public TreeNode prev = null;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null){
            return null;
        }
        ConvertChild(pRootOfTree);
        TreeNode head = pRootOfTree;
        while(head.left!=null){
            head = head.left;
        }
        return head;
    }
    public void ConvertChild(TreeNode root){
        if (root == null){
            return;
        }
        ConvertChild(root.left);
        root.left = prev;
        if (prev != null){
            prev.right = root;
        }
        prev = root;
        ConvertChild(root.right);
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null){
            return null;
        }
        if (root == p || root == q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        if (leftTree != null && rightTree != null){
            return root;
        }else if(leftTree != null){
            return leftTree;
        }else{
            return rightTree;
        }
    } public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if (root == null){
            return false;
        }
        stack.push(root);
        if (root == node){
            return true;
        }
        boolean ret = getPath(root.left,node,stack);
        if (ret) return true;
        ret = getPath(root.right,node,stack);
        if (ret) return true;
        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        Stack<TreeNode> stackP = new Stack();
        Stack<TreeNode> stackQ = new Stack();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);
        int size1 = stackP.size();
        int size2 = stackQ.size();
        if (size1 > size2){
            int size = size1 - size2;
            while(size != 0){
                size--;
                stackP.pop();
            }
        }else{
            int size = size2 - size1;
            while(size != 0){
                size--;
                stackQ.pop();
            }
        }
        while (!stackP.isEmpty() && !stackQ.isEmpty()){
            if(stackP.peek()==stackQ.peek()){
                return stackP.peek();
            }else{
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }

}
